package com.xmq.webdemo;

import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SendOpenDoor extends Thread {
    private String TAG = SendOpenDoor.class.getSimpleName();
    @Override
    public void run() {
        int controllerPort = 60000; //60000;

        long sn =153165108;
        byte[] byteCmd = new byte[] { (byte) 0x37, (byte) 0x40,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                (byte) 0x00, (byte) 0x00 };

        byteCmd[0] = (byte)0x17;
        byteCmd[1] = (byte)0x94;

//        byteCmd[4] = (byte) (sn & 0xff);
//        Log.i(TAG,"进行与操作后获取的值是:"+byteCmd[4]);
//        byteCmd[5] = (byte) ((sn >> 8) & 0xff);
//        byteCmd[6] = (byte) ((sn >> 16) & 0xff);
//        byteCmd[7] = (byte) ((sn >> 24) & 0xff);
//        byteCmd[8] = (byte) 1;
        int tag = 0x70000001;
        byteCmd[40] = (byte) (tag & 0xff);
        byteCmd[41] = (byte) ((tag >> 8) & 0xff);
        byteCmd[42] = (byte) ((tag >> 16) & 0xff);
        byteCmd[43] = (byte) ((tag >> 24) & 0xff);
        byte[]content = null;
        DatagramSocket dataSocket = null;
        try {
            dataSocket = new DatagramSocket();
            dataSocket.setSoTimeout(1000);
            DatagramPacket snddataPacket = new DatagramPacket((byteCmd), byteCmd.length,
                    InetAddress.getByName("192.168.10.254"), controllerPort);
            dataSocket.send(snddataPacket);
            byte recvDataByte[] = new byte[64];

            Thread.sleep(200);
            DatagramPacket dataPacket = new DatagramPacket(recvDataByte,
                    recvDataByte.length);
            dataSocket.receive(dataPacket);

            content = dataPacket.getData();
            Log.i(TAG,"接收到的消息长度是:"+content[0]);


            dataSocket.close();


        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
